package cn.learn.controller.system.dept;

import cn.learn.base.BaseController;
import cn.learn.domain.system.dept.Dept;
import cn.learn.service.system.dept.DeptService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    /**
     * 分页展示全部
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "展示全部部门")
    public String deptList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo resultList = deptService.selectAllByPage(pageNum, pageSize, companyId);
        request.setAttribute("pageList", resultList);
        return "system/dept/dept-list";
    }

    /**
     * 添加部门 与 展示部门
     * @param dept
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加部门")
    public String addOne(Dept dept) {
        if (request.getMethod().equals("POST")) {
            String id = UUID.randomUUID().toString().replace("-", "");
            dept.setId(id);
            dept.setCompanyId(companyId);
            dept.setCompanyName(companyName);
            deptService.addOne(dept);
            return "redirect:/system/dept/list.do";
        }
        List<Dept> resultData = deptService.selectAll(companyId);
        request.setAttribute("deptList", resultData);
        return "system/dept/dept-add";
    }

    /**
     * 更改部门信息 与 跳转到更改信息界面
     * @param dept
     * @return
     */
    @RequestMapping(value = "/updateBuId", name = "修改部门")
    public String updateBuId(Dept dept) {
        if (request.getMethod().equals("POST")) {
            dept.setCompanyId(companyId);
            dept.setCompanyName(companyName);
            deptService.updateBuId(dept);
            return "redirect:/system/dept/list.do";
        }
        Dept resultDept = deptService.findOneById(dept);
        List<Dept> resultData = deptService.selectAll(companyId);
        request.setAttribute("data", resultDept);
        request.setAttribute("deptList", resultData);
        return "system/dept/dept-update";
    }

    /**
     * 删除一条数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", name = "删除部门")
    public String delete(String id) {
        deptService.deleteBuId(id);
        return "redirect:/system/dept/list.do";
    }

}
